<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Inventory\Mapper;

use App\Inventory\Model\InventoryGoods;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 商品信息Mapper类
 */
class InventoryGoodsMapper extends AbstractMapper
{
    /**
     * @var InventoryGoods
     */
    public $model;

    public function assignModel()
    {
        $this->model = InventoryGoods::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 商品编号
        if (isset($params['number']) && $params['number'] !== '') {
            $query->where('number', 'like', '%'.$params['number'].'%');
        }

        // 商品名称
        if (isset($params['name']) && $params['name'] !== '') {
            $query->where('name', 'like', '%'.$params['name'].'%');
        }

        // 牌号
        if (isset($params['mark']) && $params['mark'] !== '') {
            $query->where('mark', 'like', '%'.$params['mark'].'%');
        }

        // 状态
        if (isset($params['state']) && $params['state'] !== '') {
            $query->where('state', 'like', '%'.$params['state'].'%');
        }

        // 规格
        if (isset($params['size']) && $params['size'] !== '') {
            $query->where('size', 'like', '%'.$params['size'].'%');
        }

        // 计重单位
        if (isset($params['unit']) && $params['unit'] !== '') {
            $query->where('unit', '=', $params['unit']);
        }

        // 最低库存重量
        if (isset($params['min_inventory']) && $params['min_inventory'] !== '') {
            $query->where('min_inventory', '=', $params['min_inventory']);
        }

        // 最高库存重量
        if (isset($params['max_inventory']) && $params['max_inventory'] !== '') {
            $query->where('max_inventory', '=', $params['max_inventory']);
        }

        // 加价费用(元)
        if (isset($params['raise_price']) && $params['raise_price'] !== '') {
            $query->where('raise_price', '=', $params['raise_price']);
        }

        // 备注
        if (isset($params['remark']) && $params['remark'] !== '') {
            $query->where('remark', '=', $params['remark']);
        }

        // 企业ID
        if (isset($params['firmid']) && $params['firmid'] !== '') {
            $query->where('firmid', '=', $params['firmid']);
        }

        return $query;
    }
}